<?php


namespace app\store\controller\shop;

use app\store\model\Course as CourseModel;
use app\store\controller\Controller;
use app\store\model\store\Shop as ShopModel;
use app\store\model\store\shop\Coach;
use think\Config;

/**
 * 课程类
 * Class Course
 * @package app\store\controller
 */
class Course extends Controller
{
    public function index(){
        $model = new CourseModel();
        $shopList = $this->shopList();
        $list = $model->getList(array_merge(['status' => -1], $this->request->param()));
        // 商品分类
        return $this->fetch('index', compact('list','shopList'));
    }

    /**
     * 添加商品
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            $shopList = $this->shopList();
            $coachList = $this->coachList();
            $week = Config::get('week');
            return $this->fetch(
                'add',
                compact('shopList','coachList','week')
            );
        }
        $model = new CourseModel();
        if ($model->add($this->postData('goods'))) {
            return $this->renderSuccess('添加成功', url('shop.course/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 商品编辑
     * @param $goods_id
     * @return array|bool|mixed
     */
    public function edit($goods_id)
    {
        // 商品详情
        $model = CourseModel::detail($goods_id);

        $time = explode('~',$model->time);
        $course_week = array_column($model->toArray()['week'],'week_id');
        $model->start = $time[0];
        if ($model->type['value'] == 2){
            $model->end = $time[1];
        }else{
            $model->end = '';
        }

        if (!$this->request->isAjax()) {
            $shopList=$this->shopList();
            $week = Config::get('week');
            return $this->fetch(
                'edit',
                array_merge(compact('model','shopList','week','course_week'))
            );
        }
        // 更新记录
        if ($model->edit($this->postData('goods'),$model->toArray())) {
            return $this->renderSuccess('更新成功', url('shop.course/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 删除商品
     * @param $goods_id
     * @return array
     */
    public function delete($goods_id)
    {
        // 商品详情
        $model = CourseModel::detail($goods_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    public function state($goods_id, $state)
    {
        // 商品详情
        $model = CourseModel::detail($goods_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }


    /**
     * // 门店列表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopList(){
        $shop_id = $this->store['user']['shop_user_id'];
        $where=[];
        if ($shop_id){
            $where['shop_id']=$shop_id;
        }
        return ShopModel::getAllList($where);
    }

    public function coachList(){
        //所属门店的教练
        $shop_id = $this->store['user']['shop_user_id'];
        $where=[];
        if ($shop_id){
            $where['shop_id']=$shop_id;
        }
        $model = new Coach();
        return $model->getAllList($where);
    }
}